extends Area2D

@export var speed = 400  
var screen_size
#隐藏信号
signal hit

signal PlaySudi(body: RigidBody2D)
signal DR_JD(JD:float)

func _ready() -> void:
	screen_size = get_viewport_rect().size
	


func _process(delta: float) -> void:
	#当前金纬度
	var velocity = Vector2.ZERO
	
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	elif Input.is_action_pressed("move_right"):
		velocity.x += 1
	elif Input.is_action_pressed("move_up"):
		velocity.y -= 1
	elif Input.is_action_pressed("move_down"):
		velocity.y += 1
	
	if velocity.length() > 0 :
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
	
	position += velocity * delta
	position = position.clamp(Vector2.ZERO,screen_size)
	
	#制作选择动画
	if velocity.x != 0 :
		#左右
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_v = false
		$AnimatedSprite2D.flip_h = velocity.x < 0
		
	elif velocity.y != 0:
		$AnimatedSprite2D.animation = "up"
		$AnimatedSprite2D.flip_v = velocity.y > 0


func start(pos):
	position = pos
	show()
	$CollisionShape2D.disabled = false


func _on_body_entered(body: Node2D) -> void:
	hide()
	hit.emit()
	$Suo_di.game_over()
	$CollisionShape2D.set_deferred("disabled",true)
 
 

func _on_suo_di_sudi(body: RigidBody2D) -> void:
	
	PlaySudi.emit(body)

#角度
func _on_suo_di_dr_jd(JD: float) -> void:
	print("向",JD,"开炮")
	DR_JD.emit(JD)
